﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Presentation.Module.Security.Interfaces.ViewModels;
using Presentation.Module.Security.Model;
using System.Windows.Input;
using Microsoft.Practices.Prism.Regions;
using Presentation.Module.Security.Interfaces.Services;
using Microsoft.Practices.Prism.Commands;
using System.ComponentModel;
using Presentation.Module.Security.Interfaces.Views;
using Presentation.Shell.Interfaces;

namespace Presentation.Module.Security.ViewModels
{
    public class AddRoleViewModel : UserViewModelBase, IAddRoleViewModel, INotifyPropertyChanged
    {
        private Role _currentRole;

        public ICommand AddRole { get; private set; }

        public AddRoleViewModel(IRegionManager regionManager, IUserService userService)
            : base(regionManager, userService)
        {
            this.AddRole = new DelegateCommand(this.OnAddRole);

            _currentRole = new Role();

        }

        public Role CurrentRole
        {
            get
            {
                return this._currentRole;
            }

            set
            {
                if (value == null)
                {
                    return;
                }

                this._currentRole = value;
            }
        }

        public void OnAddRole()
        {
            this._userService.AddRole(this._currentRole);
            this._currentRole = new Role();
            this.ExitView();
        }

        protected void RaiseOnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void ExitView()
        {
            if (this.Exit != null)
            {
                this.Exit.Invoke();
            }

        }

        public event ViewExit Exit;

        public IAddRoleView View { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
